package com.th.supcom.pds.drugreturn.client.widget;

import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.th.supcom.pds.auditprescription.client.model.PdsPrescDetailCombineModel;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.widget.PrescDetailGrid;
import com.th.supcom.pds.common.client.widget.PrescDetailGrid.ColumnConfigEnhance;
import com.th.supcom.pds.common.enums.BatchNumberManagerFlag;
import com.th.supcom.pds.internal.client.widget.PhamBatchCombox;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * @author yzhang
 */
public class DrugReturnPrescDetailPanel extends LayoutContainer
{
    private PhamBatchCombox m_combox;

    /** 处方明细表格. */
    private PrescDetailGrid <PdsPrescDetailInfoModel> m_prescDetailGrid;

    public DrugReturnPrescDetailPanel ()
    {
        setBorders (true);
        setLayout (new FitLayout ());
        initUI ();
    }

    private CellEditor cellEditorForDate ()
    {
        DateField dateField = new DateField ();
        dateField.setPropertyEditor (new DateTimePropertyEditor (CodeConstants.DATE_FORMAT_YYYY_MM_DD));
        return new CellEditor (dateField)
        {
            @Override
            public Object postProcessValue (Object objvalue)
            {
                Object obj = objvalue;
                if (null == obj)
                {
                    return obj;
                }
                return obj;
            }

            @Override
            public void startEdit (final Element el, final Object value)
            {
                if (BatchNumberManagerFlag.UnBatchManager.getEnumCode ().equals (m_prescDetailGrid.getGrid ().getSelectionModel ().getSelectedItem ().getBatchNumberManagerFlag ()))
                {
                    super.hide ();
                    return;
                }
                else
                {
                    super.startEdit (el, value);
                }
            }
        };
    }

    private CellEditor createCellEditor ()
    {
        m_combox.addSelectionChangedListener (new SelectionChangedListener <PdsSupplyDetailModel> ()
        {
            @Override
            public void selectionChanged (SelectionChangedEvent <PdsSupplyDetailModel> se)
            {
                if (se.getSelectedItem () == null)
                {
                    return;
                }
                m_prescDetailGrid.getGrid ().getSelectionModel ().getSelectedItem ().setReceiptDate (se.getSelectedItem ().getReceiptDate ());
            }
        });
        return new CellEditor (m_combox)
        {
            @Override
            public Object postProcessValue (final Object value)
            {
                if (null == value)
                {
                    return value;
                }
                return ((ModelData) value).get ("phamBatchNumber");
            }

            @Override
            public Object preProcessValue (final Object value)
            {
                if (null == value)
                {
                    return value;
                }

                final BaseModelData data = new BaseModelData ();
                data.set ("phamBatchNumber", value);
                return data;
            }

            @Override
            public void startEdit (final Element el, final Object value)
            {
                if (BatchNumberManagerFlag.UnBatchManager.getEnumCode ().equals (m_prescDetailGrid.getGrid ().getSelectionModel ().getSelectedItem ().getBatchNumberManagerFlag ()))
                {
                    super.hide ();
                    return;
                }
                else
                {
                    final PdsPrescDetailInfoModel detailInfoModel = m_prescDetailGrid.getGrid ().getStore ().getAt (super.row);
                    final PdsPrescDetailCombineModel combinemodel = new PdsPrescDetailCombineModel ();
                    combinemodel.setPerformedBy (detailInfoModel.getPerformedBy ());
                    combinemodel.setPhamCode (detailInfoModel.getPhamStdCode ());

                    //m_combox.loadData (combinemodel);
                    super.startEdit (el, value);
                }
            }
        };
    }

    private ColumnConfigEnhance createColumnConfigEnhance ()
    {
        return new ColumnConfigEnhance ()
        {
            @Override
            public void enhance (final List <ColumnConfig> configs)
            {
                configs.remove (5);
                configs.remove (5);
                configs.remove (7);
                configs.get (3).setWidth (60);
                configs.get (4).setWidth (60);
                configs.get (5).setWidth (50);
                configs.get (6).setWidth (50);
                ColumnConfig column = new ColumnConfig ();
                SimpleMethod.setColumnConfig (column, "drugRepercussionMemo", "退药原因", HorizontalAlignment.LEFT, 90, null);
                configs.add (column);

                column = new ColumnConfig ();
                SimpleMethod.setColumnConfig (column, "phamBatchNumber", "批次", HorizontalAlignment.LEFT, 60, generatorRenderForNumber ());
                m_combox = new PhamBatchCombox (60);
                m_combox.setEditable (false);

                column.setEditor (createCellEditor ());
                configs.add (column);

                column = new ColumnConfig ();
                SimpleMethod.setColumnConfig (column, "receiptDate", "有效期", HorizontalAlignment.LEFT, 70, generatorRenderForDate ());
                column.setDateTimeFormat (DateTimeFormat.getFormat (CodeConstants.DATE_FORMAT_YYYY_MM_DD));
                column.setEditor (cellEditorForDate ());
                configs.add (column);
            }
        };
    }

    private GridCellRenderer <PdsPrescDetailInfoModel> generatorRenderForDate ()
    {
        return new GridCellRenderer <PdsPrescDetailInfoModel> ()
        {
            @Override
            public Object render (final PdsPrescDetailInfoModel model, final String property, final ColumnData config, final int rowIndex, final int colIndex,
                                  final ListStore <PdsPrescDetailInfoModel> store, final Grid <PdsPrescDetailInfoModel> grid)
            {
                if (BatchNumberManagerFlag.OnBatchManager.getEnumCode ().equals (model.getBatchNumberManagerFlag ()))
                {
                    config.cellAttr = "style='background-color:#E6E6F2; height:16px'";
                }
                return null;
            }
        };
    }

    private GridCellRenderer <PdsPrescDetailInfoModel> generatorRenderForNumber ()
    {
        return new GridCellRenderer <PdsPrescDetailInfoModel> ()
        {

            @Override
            public Object render (final PdsPrescDetailInfoModel model, final String property, final ColumnData config, final int rowIndex, final int colIndex,
                                  final ListStore <PdsPrescDetailInfoModel> store, final Grid <PdsPrescDetailInfoModel> grid)
            {
                if (BatchNumberManagerFlag.OnBatchManager.getEnumCode ().equals (model.getBatchNumberManagerFlag ()))
                {
                    config.cellAttr = "style='background-color:#E6E6F2; height:16px'";
                }
                return null;
            }
        };
    }

    public PrescDetailGrid <PdsPrescDetailInfoModel> getPrescDetailGrid ()
    {
        return m_prescDetailGrid;
    }

    /**
     * 初始化UI界面
     */
    private void initUI ()
    {
        m_prescDetailGrid = new PrescDetailGrid <PdsPrescDetailInfoModel> (createColumnConfigEnhance ());
        m_prescDetailGrid.getGrid ().getView ().setForceFit (false);
        m_prescDetailGrid.getGrid ().getStore ().addListener (Store.Add, new Listener <StoreEvent <PdsPrescDetailInfoModel>> ()
        {
            @Override
            public void handleEvent (final StoreEvent <PdsPrescDetailInfoModel> be)
            {
                boolean isHidden = true;
                for (final PdsPrescDetailInfoModel model : be.getModels ())
                {
                    if (BatchNumberManagerFlag.OnBatchManager.getEnumCode ().equals (model.getBatchNumberManagerFlag ()))
                    {
                        isHidden = false;
                        break;
                    }
                }
                final ColumnConfig phamBatchNumberColumn = m_prescDetailGrid.getGrid ().getColumnModel ().getColumnById ("phamBatchNumber");
                final ColumnConfig receiptDateColumn = m_prescDetailGrid.getGrid ().getColumnModel ().getColumnById ("receiptDate");
                phamBatchNumberColumn.setHidden (isHidden);
                receiptDateColumn.setHidden (isHidden);
                m_prescDetailGrid.getGrid ().getView ().refresh (true);
            };
        });
        add (m_prescDetailGrid);
    }
}
